package com.atguigu.order.controller;

import com.atguigu.common.utils.R;
import com.atguigu.order.entity.OrderEntity;
import com.atguigu.order.entity.OrderReturnReasonEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.UUID;

/**
 * description: RabbitController
 * date: 2022/12/20 22:54
 * author: MR.孙
 */
@RestController
@Slf4j
public class RabbitController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/sendmq")
    public R sendmq() {

        for (int i = 0; i < 10; i++) {
            OrderReturnReasonEntity orderReturnReason = new OrderReturnReasonEntity();
            orderReturnReason.setId(1L);
            orderReturnReason.setName("哈哈" + i);
            orderReturnReason.setCreateTime(new Date());
            rabbitTemplate.convertAndSend("hello.java.directExchange", "helloQueue",
                    orderReturnReason, new CorrelationData(UUID.randomUUID().toString()));
        }
        log.info("消息发送成功");
        return R.ok();
    }

    @GetMapping("/sendMq")
    public R sendMq(@RequestParam(value = "num", defaultValue = "10") Integer num) {

        for (int i = 0; i < num; i++) {
            if (i % 2 == 0) {
                OrderReturnReasonEntity orderReturnReason = new OrderReturnReasonEntity();
                orderReturnReason.setId(1L);
                orderReturnReason.setName("哈哈" + i);
                orderReturnReason.setCreateTime(new Date());
                rabbitTemplate.convertAndSend("hello.java.directExchange", "helloQueue", orderReturnReason);
            } else {
                OrderEntity orderEntity = new OrderEntity();
                orderEntity.setOrderSn(UUID.randomUUID().toString());
                // rabbitTemplate.convertAndSend("hello.java.directExchange", "helloQueue", orderEntity);
                rabbitTemplate.convertAndSend("hello.java.directExchange", "helloQueue", orderEntity);


            }
        }
        log.info("消息发送成功");
        return R.ok();
    }


}
